﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Ext.Direct.Mvc
{
    internal class RequestDataConverter : JsonConverter
    {
        public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer) {
            var data = new List<object>();
            var dataArray = JToken.ReadFrom(reader);

            if (!dataArray.HasValues) return null;

            foreach (JToken dataItem in dataArray) {
                if (dataItem is JObject) {
                    data.Add(dataItem);
                } else if (dataItem is JValue) {
                    data.Add((dataItem as JValue).Value);
                } else {
                    throw new ArgumentException("Arguments of this type are not supported.");
                }
            }

            return data.ToArray();
        }

        public override bool CanConvert(Type objectType) {
            return true;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
            throw new NotImplementedException();
        }
    }
}
